/*
	This file is part of the OdinMS Maple Story Server
    Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc> 
                       Matthias Butz <matze@odinms.de>
                       Jan Christian Meyer <vimes@odinms.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package client.messages.commands;

import static client.messages.CommandProcessor.getNamedDoubleArg;
import static client.messages.CommandProcessor.getNamedIntArg;
import static client.messages.CommandProcessor.getOptionalIntArg;
import client.MapleClient;
import client.messages.Command;
import client.messages.CommandDefinition;
import client.messages.IllegalCommandSyntaxException;
import server.life.MapleLifeFactory;
import server.life.MapleMonster;
import server.life.OverrideMonsterStats;

public class SpawnMonsterCommand implements Command {

    @Override
    public void execute(MapleClient c, String[] splitted) throws Exception, IllegalCommandSyntaxException {
	final int mid = Integer.parseInt(splitted[1]);
	final int num = Math.min(getOptionalIntArg(splitted, 2, 1), 50);

	Integer hp = getNamedIntArg(splitted, 1, "hp");
	Integer exp = getNamedIntArg(splitted, 1, "exp");
	Double php = getNamedDoubleArg(splitted, 1, "php");
	Double pexp = getNamedDoubleArg(splitted, 1, "pexp");

	final MapleMonster onemob = MapleLifeFactory.getMonster(mid);

	int newhp = 0, newexp = 0;
	final double oldExpRatio = ((double) onemob.getHp() / onemob.getMobExp());

	if (hp != null) {
	    newhp = hp.intValue();
	} else if (php != null) {
	    newhp = (int) (onemob.getMobMaxHp() * (php.doubleValue() / 100));
	} else {
	    newhp = onemob.getMobMaxHp();
	}
	if (exp != null) {
	    newexp = exp.intValue();
	} else if (pexp != null) {
	    newexp = (int) (onemob.getMobExp() * (pexp.doubleValue() / 100));
	} else {
	    newexp = onemob.getMobExp();
	}

	if (newhp < 1) {
	    newhp = 1;
	}
	final double newExpRatio = ((double) newhp / newexp);

	c.getPlayer().dropMessage(6, "Ratio : (" + newExpRatio + " < " + oldExpRatio + ")");

	if (newExpRatio < oldExpRatio * 3 && newexp > 0) {
	    c.getPlayer().dropMessage(6, "You are not allowed to spawn a better monster of 4x ratio than the original.");
	    return;
	} else if (c.getPlayer().getGMLevel() <= 2) {
	    if (mid == 8810018 || mid == 9400203) {
		c.getPlayer().dropMessage(6, "This monster is blocked.");
		return;
	    }
	}
	final OverrideMonsterStats overrideStats = new OverrideMonsterStats();
	overrideStats.setOHp(newhp);
	overrideStats.setOExp(newexp);
	overrideStats.setOMp(onemob.getMobMaxMp());

	for (int i = 0; i < num; i++) {
	    MapleMonster mob = MapleLifeFactory.getMonster(mid);
	    mob.setHp(newhp);
	    mob.setOverrideStats(overrideStats);
	    c.getPlayer().getMap().spawnMonsterOnGroundBelow(mob, c.getPlayer().getPosition());
	}
    }

    @Override
    public CommandDefinition[] getDefinition() {
	return new CommandDefinition[]{
		    new CommandDefinition("spawn", "monsterid [hp newHp] [exp newExp] [php procentual Hp] [pexp procentual Exp]", "Spawns the monster with the given id", 2)
	};
    }
}
